// import {Address} from '../../utils/address.js';
import { Subscribe } from '../subscribe/subscribe-model.js';
import { My } from '../my/my-model.js';
import { Follow } from '../follow/follow-model.js';


var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
var demo = new QQMapWX({
  key: 'JCQBZ-DZQ6P-ZJEDL-LROIS-3K6Y2-3NF6R' // 必填
});
var base64 = require("../../utils/base64");
var sliderWidth = 60;
// var address=new Address();
// var order=new Order();
var my = new My();
var subscribe = new Subscribe();
var follow = new Follow();
var userInfoData;

Page({
  data: {
    pageIndex: 1,
    isLoadedAll: false,
    loadingHidden: false,
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: 0,
    tabs: [
      { "name": "我的关注", "num": '' },
      { "name": "我的预约", "num": '' },
      { "name": "我的推荐", "icon": 'iconfont icon-iconfonttuijianren' },
      { "name": "我的收入", "icon": 'iconfont icon-lishishouru' }
    ],
    activeIndex: 1,
    sliderOffset: 0,
    sliderLeft: 0,
    isScrollY: true,
    //代理
    agent: "../../imgs/my/agent.png",
    //预约
    appointment: "../../imgs/my/appointment.png",
    //关注
    attentionyeticon: "../../imgs/my/attentionyeticon.png",
    //卡券
    bankcard: "../../imgs/my/bankcard.png",
    //常见问题
    feedback: "../../imgs/my/feedback.png",
    //意见反馈
    problem: "../../imgs/my/problem.png",
    //了解爱车易得
    understand: "../../imgs/my/understand.png",
    //联系客服
    customer_service: "../../imgs/my/customer_service.png",
    //设置
    Setupthe: "../../imgs/my/Setupthe.png",
    useData: ''
    // //余额、银行卡图标
    // balance: "../../imgs/icon/balance.png",
    // bankcard:"../../imgs/icon/bankcard.png",

    // lenHeight:1000

    // orderArr:[],
    // addressInfo:null
  },
  onLoad: function () {

    var that = this;

    that._loadData();




    // wx.getSystemInfo({
    //   success: function (res) {

    //     that.setData({
    //       sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
    //       sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
    //     });
    //   }
    // });

    // this._getAddressInfo();
  },
  // onPageScroll: function (obj) {
  //   let scrollTop = obj.scrollTop;
  //   if (scrollTop > 80) {
  //     this.setData({
  //       fixed: 'position: fixed'
  //     })
  //   } else {
  //     this.setData({
  //       fixed: ''
  //     })
  //   }
  // },




  _loadData: function () {
    var that = this;


    //  this.data.tabs[0].num = 
    my.getUserInfo((data) => {
      my.updateUserInfo(data, (datas) => {
        console.log(datas)

      })
      // console.log(data)
      this.data.useData = data
      that.setData({
        userInfo: data,
        loadingHidden: true
      });
    });


    wx.getSetting({
      success: (ress) => {

        // console.log(ress.authSetting['scope.userInfo']);
        if (!ress.authSetting['scope.userInfo']) {
          wx.openSetting({
            success: (res) => {
              if (res.authSetting['scope.userInfo'] == true) {
                this._loadData();
                wx.switchTab({
                  url: '../my/my'
                });
              }
            }

          })
        } else {

          wx.getUserInfo({
            success: function (res) {

              var userInfo = res.userInfo
              var nickName = userInfo.nickName
              var avatarUrl = userInfo.avatarUrl
              var gender = userInfo.gender //性别 0：未知、1：男、2：女
              var province = userInfo.province
              var city = userInfo.city
              var country = userInfo.country

            }
          })

          subscribe.getSubscribeAll((data) => {

            var len = 0;
            if (data == 0) {
              len = 1000
            } else {
              len = data.length * 1000
            }

            that.setData({
              storeTypeArr: data,
              loadingHidden: true,
              // 预约数量
              subscribeNum: data.length,
              sColor: true,
              fColor: false,
              lenHeight: len
            });

          });
          //显示关注的数量
          follow.catFollowData((data) => {




            that.setData({
              floowNum: data.length
            });

            // this._loadData();
          });

        }
      }
    })

    wx.getSystemInfo({

      success: function (res) {
        // console.log(res);
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
    // this._getOrders();
    // order.execSetStorageSync(false);  //更新标志位
  },
  onShow: function () {

    var page = getCurrentPages().pop();
    if (page == undefined || page == null) return;
    page._loadData();

  },

  //跳转到银行卡页面
  bankInfo: function (event) {
    wx.switchTab({
      url: '../bank/bank'
    })
  },


  /*跳转到商品详情*/
  onProductsItemTap: function (event) {
    var id = my.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../product/product?id=' + id
    })
  },

  /*下拉刷新页面*/
  onPullDownRefresh: function () {
    this._loadData(() => {
      wx.stopPullDownRefresh()
    });
  },


  detail: function (event) {

    wx.switchTab({
      url: '../category/category'
    });
    //接口调用成功的回调方法
    //             fuccess: function () { },
    //        / 接口调用失败的回调方法
    //        fail: function () { },
    //             /接口调用无论成功或者失败的回调方法
    //        complete: function () { }

  },

  /*
   * 提示窗口
   * params:
   * title - {string}标题
   * content - {string}内容
   * flag - {bool}是否跳转到 "我的页面"
   */
  showTips: function (title, content) {

    wx.showModal({
      title: title,
      content: content,
      showCancel: false,
      success: function (res) {

      }
    });
  },

  // bindChange: function (e) {

  //     var that = this;
  //     that.setData({ currentTab: e.detail.current });

  // },

  mapS: function (e) {
    var that = this;
    var longitude;
    var latitude;
    demo.geocoder({
      address: e.currentTarget.dataset.name,
      success: function (ress) {
        longitude = ress.result.location.lng;
        latitude = ress.result.location.lat;
        wx.getLocation({
          // type: 'wgs84', //返回可以用于wx.openLocation的经纬度  
          success: function (res) {

            longitude: longitude;
            latitude: latitude;
            wx.openLocation({
              latitude: latitude,
              longitude: longitude,
              name: e.currentTarget.dataset.name,
              scale: 28
            })
          }
        })

      },

    });

    ;

  },
  pay: function (e) {
    var id = my.getDataSet(e, 'id');
    var that = this;
    my.paySub(id, (data) => {

      var timeStamp = data.timeStamp;
      var nonceStr = data.nonceStr;
      var p_package = data.package;
      var paySign = data.paySign;
      var signType = data.signType
      // console.log(timeStamp + '+' + nonceStr + '+' + p_package + '+' + paySign);
      wx.requestPayment(
        {
          // 'appId': data.appid,
          'timeStamp': timeStamp,
          'nonceStr': nonceStr,
          'package': p_package,
          'signType': signType,
          'paySign': paySign,
          'success': function (res) {
            that._loadData();
          },
          'fail': function (res) {
            // console.log(res)
          },
          'complete': function (res) {
            // console.log(res)
          }
        })
      // that.setData({

      // });

    });
  }

})